package org.os.framework.crypto.algorithm;

import java.security.MessageDigest;

import org.os.framework.crypto.model.AbstractHash;

import lombok.SneakyThrows;

/**
 * @author 
 */
public class Sha1 extends AbstractHash {

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String salt;

        public Builder salt(String salt) {
            this.salt = salt;
            return this;
        }

        public Sha1 build() {
            return new Sha1(this.salt);
        }
    }


    private static final String ALGORITHM = "SHA-1";

    private String salt;

    public Sha1(String salt) {
        this.salt = null == salt ? "" : salt;
    }

    @SneakyThrows
    @Override
    public String digest(String s) {
        String data = s + salt;
        byte[] bytes = data.getBytes();
        MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
        digest.update(bytes);
        byte[] d = digest.digest();
        int j = d.length;
        byte[] buf = new byte[j * 2];
        int k = 0;
        for (byte byte0 : d) {
            buf[k++] = CHARS[byte0 >>> 4 & 0xf];
            buf[k++] = CHARS[byte0 & 0xf];
        }
        return new String(buf);
    }
}
